import java.util.LinkedList;
import java.awt.Point;
/**
 * Robot in the maze universe.
 * 
 * @author Jofry HS
 * @version 16-07-2010
 */
public class Robot
{
    private int locX, locY;     // X and Y location of the BadBot
    
    // Robot keeps track of its previous locations
    private LinkedList<Point> prevLocation = new LinkedList<Point>();
    
    /**
     * Create Robot at specific x and y location
     */
    public Robot(int x, int y)
    {
        locX = x;
        locY = y;
    }
    
    // Getters
    public int getX()
    {
        return locX;
    }
    
    public int getY()
    {
        return locY;
    }
    
    // No Setters? Yes, we want to make sure programmer move the Robot by calling the move method    
    // Move methods are called by passing map (two-dimensional array), to update the existing of the 
    // new robot location
    public void moveUp(Tile[][] map)
    {
        map[locX][locY].free();
        locY--;
        map[locX][locY].becomeOccupied();
    }
    
    public void moveRight(Tile[][] map)
    {
        map[locX][locY].free();
        locX++;

        map[locX][locY].becomeOccupied();
    }
    
    public void moveDown(Tile[][] map)
    {
        map[locX][locY].free();
        locY++;

        map[locX][locY].becomeOccupied();
    }
    
    public void moveLeft(Tile[][] map)
    {
        map[locX][locY].free();
        locX--;

        map[locX][locY].becomeOccupied();
    }
    
    // Save current location into prevLocation collection
    public void saveLocation()
    {
        Point newLoc = new Point(locX,locY);
        prevLocation.add(newLoc);
    }
    
    // Return robot one step back to previous location and update the collection
    public void undo(Tile[][] map)
    {
        Point undoLoc = prevLocation.removeLast();
        map[locX][locY].free();
        locX = (int) undoLoc.getX();
        locY = (int) undoLoc.getY();
        map[locX][locY].becomeOccupied();
    }
    
    // Checks whether previous location is empty
    public boolean canUndo()
    {
        if (prevLocation.isEmpty())
            return false;
        else return true;
    }
}
